package cn.ac.iscas.action.softwareCopyright;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;


import cn.ac.iscas.bean.PageView;
import cn.ac.iscas.bean.project.Project;
import cn.ac.iscas.bean.softwareCopyright.SoftwareCopyright;
import cn.ac.iscas.service.project.IProjectService;
import cn.ac.iscas.service.softwareCopyright.ISoftwareCopyrightService;

@Controller @Scope("prototype")
public class SoftwareCopyrightAction {
	
	SoftwareCopyrightQuery softwareCopyrightQuery;
	public SoftwareCopyrightQuery getSoftwareCopyrightQuery() {
		return softwareCopyrightQuery;
	}
	public void setSoftwareCopyrightQuery(
			SoftwareCopyrightQuery softwareCopyrightQuery) {
		this.softwareCopyrightQuery = softwareCopyrightQuery;
	}

	@Resource ISoftwareCopyrightService softwareCopyrightService;
	private int page;
    
	public ISoftwareCopyrightService getProjectService() {
		return softwareCopyrightService;
	}

	public void setProjectService(ISoftwareCopyrightService softwareCopyrightService) {
		this.softwareCopyrightService = softwareCopyrightService;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	public String queryPage(){
		return "query";
	}
	public String typeIn()
	{
		return "add";
	}
	public String list()
	{
		page = (0==page)?1:page;
		PageView<SoftwareCopyright> pageView = new PageView<SoftwareCopyright>(7, page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(softwareCopyrightService.getScrollData(SoftwareCopyright.class,pageView.getFirstResult(),pageView.getMaxresult(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list";
	}
	public String query(){
		if(page<=0)page=1;
		StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		
//		if(softwareCopyrightQuery.getProjectNo()!=null && !"".equals(softwareCopyrightQuery.getProjectNo())){
//			if(params.size()>0) jpql.append(" and ");
//			jpql.append(" o.projectNo = ?");
//			params.add(softwareCopyrightQuery.getProjectNo());
//		}
//		if(softwareCopyrightQuery.getProjectName()!=null && !"".equals(softwareCopyrightQuery.getProjectName())){
//			if(params.size()>0) jpql.append(" and ");
//			System.out.println(softwareCopyrightQuery.getProjectName()+"name");
//			jpql.append(" o.projectName like ?");
//			params.add("%"+ softwareCopyrightQuery.getProjectName()+ "%");
//		}
//		if(softwareCopyrightQuery.getPersonInCharge()!=null && !"".equals(softwareCopyrightQuery.getPersonInCharge())){
//			if(params.size()>0) jpql.append(" and ");
//			jpql.append(" o.personInCharge = ?");
//			params.add(softwareCopyrightQuery.getPersonInCharge());
//		}
//		System.out.println(softwareCopyrightQuery.getReposiblePerson()+"ddd");
//		if(softwareCopyrightQuery.getReposiblePerson()!=null && !"".equals(softwareCopyrightQuery.getReposiblePerson())){
//			if(params.size()>0) jpql.append(" and ");
//			jpql.append(" o.reposiblePerson = ?");
//			params.add(softwareCopyrightQuery.getReposiblePerson());
//		}
		
		
		if(softwareCopyrightQuery.getAcceptNo()!=null && !"".equals(softwareCopyrightQuery.getAcceptNo())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.acceptNo like ?");
			params.add("%"+softwareCopyrightQuery.getAcceptNo()+"%");
		}
		if(softwareCopyrightQuery.getSoftwareName()!=null && !"".equals(softwareCopyrightQuery.getSoftwareName())){
			if(params.size()>0) jpql.append(" and ");
			System.out.println(softwareCopyrightQuery.getSoftwareName()+"name");
			jpql.append(" o.softwareName like ?");
			params.add("%"+ softwareCopyrightQuery.getSoftwareName()+ "%");
		}
		if(softwareCopyrightQuery.getCopyrightOwner()!=null && !"".equals(softwareCopyrightQuery.getCopyrightOwner())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.copyrightOwner like ?");
			params.add("%"+softwareCopyrightQuery.getCopyrightOwner()+"%");
		}
		System.out.println(softwareCopyrightQuery.getRegisterNo()+"ddd");
		if(softwareCopyrightQuery.getRegisterNo()!=null && !"".equals(softwareCopyrightQuery.getRegisterNo())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.registerNo like ?");
			params.add("%"+softwareCopyrightQuery.getRegisterNo()+"%");
		}
		System.out.println(softwareCopyrightQuery.getState());
		if(softwareCopyrightQuery.getState()!=null &&!"".equals(softwareCopyrightQuery.getState()))
		{
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.state = ?");
			System.out.println(softwareCopyrightQuery.getState());
			params.add(softwareCopyrightQuery.getState());
		}
		PageView<SoftwareCopyright> pageView=new PageView<SoftwareCopyright>(7,page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(softwareCopyrightService.getScrollData(SoftwareCopyright.class,pageView.getFirstResult(), 
				pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list" ;
		}
	    public String test() throws IOException
	    {
	    	ActionContext ctx = ActionContext.getContext();     
		    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		    response.setContentType("text/html;charset=utf-8");
		    PrintWriter out=response.getWriter();
		    response.setContentType("text/html;charset=utf-8");
		    HttpServletRequest request = ServletActionContext.getRequest();
		    System.out.println(request.getParameter("name"));
		    out.println(request.getParameter("name") + ",服务器时间：" + new Date()
		    		+ ",请校对!");
		   out.flush();
		   out.close();
		   return null;
	    }
}
